package com.yyge.mapper.impl;

import com.yyge.entity.OrderGoods;
import com.yyge.mapper.OrderGoodsMapper;
import com.yyge.util.DruidDataSourceHandler;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class OrderGoodsMapperImpl implements OrderGoodsMapper {
    @Override
    public int insert(OrderGoods orderGoods) {
        // 得到连接池
        DataSource dataSource = DruidDataSourceHandler.getDataSource();
         // 基于QueryRunner类的对象来进行
        QueryRunner queryRunner=new QueryRunner(dataSource);
        String sql="insert into order_goods"+
          " (id, order_id, goods_serial, price, goods_number, is_gift, type_parent_name, type_sub_name, goods_name, goods_img)"+
          "values (?,?,?,?,?,?,?,?,?,?)";
        Object[] params={
                orderGoods.getId(),
                orderGoods.getOrder_id(),
                orderGoods.getGoods_serial(),
                orderGoods.getPrice(),
                orderGoods.getGoods_number(),
                orderGoods.getIs_gift(),
                orderGoods.getType_parent_name(),
                orderGoods.getType_sub_name(),
                orderGoods.getGoods_name(),
                orderGoods.getGoods_img()
        };

        try {
           return queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteByPrimaryKey(Integer key) {
        String sql="delete from order_goods where id=?";
        DataSource dataSource=DruidDataSourceHandler.getDataSource();
        QueryRunner queryRunner=new QueryRunner(dataSource);
        try {
            return queryRunner.update(sql,key);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateByPrimaryKey(OrderGoods orderGoods) {
        String sql="update order_goods set "+
         "order_id=?,"+
         "goods_serial=?,"+
         "price=?,"+
         "goods_number=?,"+
         "is_gift=?,"+
         "type_parent_name=?,"+
         "type_sub_name=?"+
         "goods_name=?,"+
         "goods_img=?"+
         "where id=?";
        DataSource dataSource=DruidDataSourceHandler.getDataSource();
        QueryRunner queryRunner=new QueryRunner(dataSource);
        Object[] params= {
            orderGoods.getOrder_id(),
            orderGoods.getGoods_serial(),
            orderGoods.getPrice(),
            orderGoods.getGoods_number(),
            orderGoods.getIs_gift(),
            orderGoods.getType_parent_name(),
            orderGoods.getType_sub_name(),
            orderGoods.getGoods_name(),
            orderGoods.getGoods_img(),
            orderGoods.getId()
        };
        try {
           return queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public OrderGoods selectByPrimaryKey(Integer key) {
        String sql="select * from order_goods where id=?";
        QueryRunner queryRunner=new QueryRunner(DruidDataSourceHandler.getDataSource());
        ResultSetHandler<OrderGoods> rsh= new BeanHandler<>(OrderGoods.class);
        try {
            OrderGoods addrInfo=queryRunner.query(sql,rsh,key);
            return addrInfo;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
